package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.cache.Cache;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotNextStepDto;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/11/5
 */
@Slf4j
@LiteflowComponent(value = "WaitGoNextStep", name = "[轮对/轮轴]等待上位机可以执行下一步")
@AllArgsConstructor
public class WaitGoNextStep extends NodeComponent {

    private final RobotService robotService;

    private final RobotMapPosService robotMapPosService;

    private final RobotDriverService robotDriverService;

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        Robot robot = robotService.getDefaultRobot();
        FlowContextDto dto =  this.getRequestData();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
        //String sourceName = ticketsDto.getModalData().get("sourcePosName")== null ?
        //        String.valueOf(ticketsDto.getModalData().get("sourcePosCode")):
        //        String.valueOf(ticketsDto.getModalData().get("sourcePosName"));
        String sourceName = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();
        String taskCode=dto.getTickets().getTaskNo();
        if(sourceName.contains("LM")) {
            log.info("跳过等待");
            return;
        }
        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getCode, iRobotPosDto.getCurrentStation())
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        log.info("直接到达位子：" + robotMapPos.getName());

        String auto = ticketsDto.getModalData().get("auto")== null ? "0":
                String.valueOf(ticketsDto.getModalData().get("auto"));

        if (auto.equals("0")) {
            log.info("半自动任务:");
            return ;
        }
        log.info("起点位置编码:" + sourceName);
        Cache<String, RobotNextStepDto> nextStep = SpringUtil.getBean("nextStep");
        RobotNextStepDto nextDto = nextStep.getIfPresent(taskCode);
        while(true) {
            nextDto  = nextStep.getIfPresent(taskCode);
            if (nextDto != null && sourceName.equals(nextDto.getPosCode())) {
                log.info("等待可以下一个步骤");
                break;
            }
            nextStep = SpringUtil.getBean("nextStep");
            TimeUnit.SECONDS.sleep(1);
            LiteFlowUtil.checkInterruptFlag();
        }
        nextDto.setPosCode("-1");
        nextStep.put(taskCode,  nextDto);
    }
}
